package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.service.IBookService;
import com.woniu.service.IMemberService;
import com.woniu.vo.BookQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dong
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/book")
@Api(tags = "预约管理")
public class BookController extends BaseController{

    @Autowired
    private IBookService bookService;
    @Autowired
    private IMemberService memberService;


    @ApiOperation("搜索分页预约列表")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody BookQueryVo bookQueryVo){
        PageResult pageResult = bookService.search(currentPage, pageSize, bookQueryVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("根据id修改状态")
    @PutMapping("/update/{status}/{bookId}")
    public Result updateById(@PathVariable String bookId, @PathVariable String status){
        boolean flag = bookService.updateByStatus(bookId, status);
        return toResult(flag);
    }

    @ApiOperation("预约课程")
    @PostMapping("/book/{cardId}/{timetableId}")
    public Result book(@PathVariable String cardId, @PathVariable String timetableId, HttpSession session){
        String tel = (String) session.getAttribute("tel");
        String memberId = memberService.queryMemberId(tel);
        BigDecimal discount = (BigDecimal) session.getAttribute("discount");
        bookService.book(memberId, cardId, timetableId, discount);
        return success();
    }

}
